<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>风向标</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">						
			//p {margin:1px}
			//ul {margin:1px}			
			.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 10px;     
				height: 10px;				
			}
			table{
			width:380px; 
			border-collapse:collapse; 
			text-align:center;
			}
			td,th{border:1px solid #696969}				
		</style>		
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="DatePicker/WdatePicker.js"> </script>
		<script>
			var dt = "";
			var tt = "";
			var gt = "";
			var k = "0";
			window.onload = function() {
			var day = new Date();
			gt = day.getTime();
			mymonth = day.getMonth()+1;
			myday = day.getDate();
			if(mymonth >= 10){mymonth = mymonth;}else{mymonth = "0" + mymonth;}
			if(myday >= 10){myday = myday;}else{myday = "0" + myday;}
			tt = day.getFullYear() + "-" + mymonth + "-" + myday;	
			dt = tt;
			getSource();
			changeStyle();
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					getSource();				
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {				
				time_range("9:00","15:15")														
				},
				60000);
			})
			
			function changeStyle() {
				var obj1 = document.getElementById("towrite");				
				obj1.style.left= "0px"; 				
				obj1.style.width= "580px";	
				obj1.style.height= "620px";		
				obj1.style.fontSize="12px";				 
			}
			
			
			Date.prototype.Format = function (fmt) {
			var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
			};
			
			if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function getLocalTime(nS) {			          
			return new Date(parseInt(nS) * 1000).Format("hh:mm:ss")}
			
			function keepTwoDecimal(num) {  
               var result = parseFloat(num);  
               if (isNaN(result)) {  
                   //alert('传递参数错误，请检查！');  
                   return false;  
               }  
               result = Math.round(num * 100) / 100;  
               return result;  
			};
						
			function cDayFunc(dp) 
			{ 
				var date = dp.cal.getNewDateStr();            
				BookingRmk(date);//调用其它函数并带上参数                
			}
			
			function BookingRmk(date){         
				tt = date;
				var date1 = new Date(tt);
				var date2 = new Date(dt);					
				k = 0;
				if(date1.getTime() < date2.getTime()){			
					k = 1;
					tradeday();
				}								
				getSource();						
			}

			function tradeday() {  //判断最后交易日
				var day = new Date(tt);							
				for (i = 1; i < 100; i++){	//节假日																										
					if(i > 1){
						day.setTime(day.getTime() - 24*60*60*1000);							
					}	
					tt = day.Format("yyyy-MM-dd");
					//alert(tt);
					var url = "https://apphis.longhuvip.com/w1/api/index.php?Order=1&a=HisDaBanList&st=10&c=HisHomeDingPan&Is_st=1&PidType=2&apiv=w30&Type=6&Day=" + tt;
					var xhr = new XMLHttpRequest();	
					xhr.open("GET", url, false);//同步 
					xhr.send(null)																										
					var data = xhr.responseText;
					//document.write(data);
					if(data.indexOf('"list":[]') == -1){						
						break;
					}
				}
			}	
			
			function createXmlHttp() {
				if (window.XMLHttpRequest) {
					xmlHttp = new XMLHttpRequest();					
					} else {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");					
				}
			}
			
			<!-- var trid = ""; -->
			<!-- function changeCss(obj,hid,id){						 -->
				<!-- var tlist = document.getElementById(id).getElementsByTagName("tr");					 -->
				<!-- for(var i = 0; i < tlist.length; i++){														 -->
					<!-- tlist[i].style.backgroundColor = "#252528"; 					 -->
				<!-- } -->
				<!-- trid = hid; -->
				<!-- var tr = document.getElementById(hid);				 -->
				<!-- tr.style.backgroundColor = "#483D8B";						 -->
			<!-- } -->
			
			<!-- function hover(){  //鼠标经过 -->
				<!-- var rows = document.getElementsByTagName('tr'); -->
				<!-- for(var i=0;i<rows.length;i++){ -->
					<!-- rows[i].onmouseover=function(){						 -->
						<!-- if(this.id != ""){  //过滤表头 -->
							<!-- this.style.background="#800000"; -->
						<!-- } -->
					<!-- } -->
					<!-- rows[i].onmouseout=function(){ -->
						<!-- if(this.id != ""){  //过滤表头 -->
							<!-- if(this.id != trid){  //过滤选中行 -->
								<!-- this.style.background="#252528"; -->
							<!-- }else{ -->
								<!-- this.style.background="#483D8B"; -->
							<!-- } -->
						<!-- }						 -->
					<!-- }					 -->
				<!-- } -->
			<!-- } -->
			
			var trid = "";
			var trs = "";
			function changeCss(obj,id){	
				trs = "";
				var t = obj.parentNode.parentNode.id;  //td父节点tr id
				var ty = t.split("#")[1];
				var ty1 = "";
				var ty2 = "";
				if(ty.indexOf("、") > -1){
					ty1 = ty.split("、")[0];
					ty2 = ty.split("、")[1];
				}
				
				var tlist = document.getElementById(id).getElementsByTagName("tr");					
				for(var i = 0; i < tlist.length; i++){
					if(tlist[i].id != ""){
						var dm = tlist[i].id.split("#")[0];
						var yy = tlist[i].id.split("#")[1];
						if(t == tlist[i].id){
							tlist[i].style.backgroundColor = "#483D8B";  //选中行
							trid = t;
						}else if(ty.indexOf("、") > -1){
							if(yy.indexOf(ty1) > -1 || yy.indexOf(ty2) > -1){
								tlist[i].style.backgroundColor = "#800080"; //相同涨停原因
								trs += tlist[i].id;
							}else{
								tlist[i].style.backgroundColor = "#252528"; 				
							}	
						}else{
							if(yy.indexOf(ty) > -1){
								tlist[i].style.backgroundColor = "#800080"; //相同涨停原因
								trs += tlist[i].id;
							}else{
								tlist[i].style.backgroundColor = "#252528"; 				
							}	
						}	
					}					
				}				
			}
			
			function hover(){  //鼠标经过
				var rows = document.getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){							
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){						
						if(this.id != ""){  //过滤表头
							if(this.id == trid){  //过滤选中行
								this.style.background="#483D8B";
							}else if(trs.indexOf(this.id) > -1){
								this.style.background="#800080";
							}else{
								this.style.background="#252528";
							}
						}						
					}					
				}
			}			
			
			function getSource() {
				//alert(ss);
				div = document.getElementById('towrite');
				div.innerHTML = '<font size="3">风向标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><input style="width: 120px;" type="text" id="PriceDate" name="PriceDate" class="Wdate" onFocus="WdatePicker({ onpicking: cDayFunc})" /><br><br>';						
				
				var url1 = "https://apphq.longhuvip.com/w1/api/index.php?st=30&Order=1&PlateID=801225&a=ZhiShuStockList_W8&Type=6&c=ZhiShuRanking"; 								
				var url2 = "https://apphis.longhuvip.com/w1/api/index.php?st=30&Order=1&PlateID=801225&Token=0&a=ZhiShuStockList_W8&Type=6&c=ZhiShuRanking&Date=" + tt;				
				var url = url1;
				if(k == "1" )
				{
				  url = url2;
				}					
				createXmlHttp();					
				xmlHttp.onreadystatechange = writeSource;
				xmlHttp.open("GET", url, true); 
				xmlHttp.send(null);				
			}
			
			function writeSource() {
				if (xmlHttp.readyState == 4) { 
					var data = xmlHttp.responseText
					//document.write(data);
					if(k == 0){
						tt = data.match(/"Day":\["(\S*)"\],"Min"/)[1];
						dt = tt;
					}
					data = data.replace(/ /g,"").match(/{"list":(\S*)/)[1].split("]]")[0] + ']]';							
					data = data.replace(/(\[\").*?(?=\])/g,function(word){ 
					str = word.match(/\[(\S*)/)[1];				
					return '{"code":' + str.split(',')[0] + 
					',"name":' + str.split(',')[1] +
					',"xj":' + str.split(',')[5] +
					',"zf":'+str.split(',')[6] +
					',"zs":'+str.split(',')[9]+
					',"zl":'+str.split(',')[2] +
					',"lb":'+str.split(',')[23] + 
					',"bk":'+str.split(',')[4];});
					data = data.replace(/\[\[/g,'[{').replace(/\]\]/g,"}]").replace(/],{/g,"},{");
					//data = '{\"list\":' + data.replace(/\],\{/g,"},{").replace(/\]\]/g,"}]") + "}";
					data = data.replace(/\],\{/g,"},{").replace(/\]\]/g,"}]");
					//document.write(data);
					
					var json = JSON.parse(data);					
					
					//创建table					
					var table=document.createElement("table");										
					var thead=document.createElement("thead");
					table.appendChild(thead);					
					var tr=document.createElement("tr");
					thead.appendChild(tr);					
					
					var m = 0;
					var n = 0;
					var b = 0;
					var d = 0;
					var gl = "涨幅";  //高亮
					for(var key in json[0]){  //表头
						key = key.replace('code','代码').replace('name','名称').replace('zf','涨幅%').replace('xj','现价').replace('zl','主力');
						key = key.replace('zs','涨速%').replace('lb','连板').replace('bk','板块');
						var th=document.createElement("th");						
						th.innerHTML=key;						
						tr.appendChild(th);
						if(gl == key.replace("%","")){
							n = m;
							th.style.color = "#FF6633";	
						}
						if(key == "主力"){
							b = m;
							th.style.color = "#FFA500";	
						}else if(key == "连板"){
							th.style.color = "#FFA500";	
						}else if(key == "板块"){
							d = m;
							th.style.color = "#FFA500";	
						}
						m++;
					}							
					var tbody=document.createElement("tbody");					
					table.appendChild(tbody);
					
					//遍历json
					for(var i = 0; i < json.length; i++){					
						var tr=document.createElement("tr");						
						var p = 1;
						var dm = "";
						var mc = "";
						for(var key in json[i]){ //行元素遍历						
							var td=document.createElement("td");							
							
							var rc = json[i][key];
							if(p == 1){  //代码列
								dm = rc;
								tr.id = dm;
							}else if(p == 5){
								rc = keepTwoDecimal(rc);
							}else if(p == d + 1){
								zt = rc;
								tr.id = dm + "#" + zt;
							}						
							//td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"" + dm + "\",\"gg\")'>" + rc + "</a>";
							td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"gg\")'>" + rc + "</a>";

							if(p == n + 1){							
								td.getElementsByTagName("a")[0].style.color = "#FF6633";
							}
							if(p >= b + 1){							
								td.getElementsByTagName("a")[0].style.color = "#FFA500";
							}																
							tr.appendChild(td);							
							p++;
						}													
						tbody.appendChild(tr);																
					}					
					var div = document.getElementById("towrite");									
					div.appendChild(table);
					table.id = "gg";
					
					var table=document.getElementsByTagName("table")[0];
					makeSortable(table);			
					hover();					
				}	
				document.getElementById("PriceDate").value = tt;
				//hover();				
			}
			
			function makeSortable(table) {
				var headers=table.getElementsByTagName("th");
				for(var i=0;i<headers.length;i++){				
					(function(n){
						var flag=false;
							
						headers[n].onclick=function(){												
							showCol(n);		//高亮
							var tbody=table.tBodies[0];
							var rows=tbody.getElementsByTagName("tr");
							rows=Array.prototype.slice.call(rows,0);

							//基于第n个<td>元素的值对行排序
							rows.sort(function(row1,row2){
								headtxt = headers[n].textContent;								
								var cell1=row1.getElementsByTagName("td")[n];//获得第n个单元格
								var cell2=row2.getElementsByTagName("td")[n];																
								var val1=cell1.textContent||cell1.innerText;//获得文本内容
								var val2=cell2.textContent||cell2.innerText;
								if(headtxt.indexOf("名称") != -1 || headtxt.indexOf("主力") != -1 || headtxt.indexOf("连板") != -1 || headtxt.indexOf("板块") != -1){
									val1 = val1;
									val2 = val2;
								}else{
									if(val1 == "-"){
										val1 = " ";
									}
									if(val2 == "-"){
										val2 = " ";
									}
									val1 = val1.replace(" ","0").replace("无","1000").replace("NaN","0");
									val2 = val2.replace(" ","0").replace("无","1000").replace("NaN","0");
									if(val1.indexOf("万") != -1){
										val1 = parseFloat(val1)*10000;
									}else if(val1.indexOf("亿") != -1){
										val1 = parseFloat(val1)*100000000;
									}else{
										val1 = parseFloat(val1);
									}
									
									if(val2.indexOf("万") != -1){
										val2 = parseFloat(val2)*10000;
									}else if(val2.indexOf("亿") != -1){
										val2 = parseFloat(val2)*100000000;
									}else{
										val2 = parseFloat(val2);
									}
								}
								if(val1<val2){
									return -1;
								}else if(val1>val2){
									return 1;
								}else{
									return 0;
								}
							});
							if(flag){
								rows.reverse();
							}
							
							for(var i=0;i<rows.length;i++){
								tbody.appendChild(rows[i]);
							}

							flag=!flag;
						}
					}(i));
				}
			}
			
			function showCol(iCol){			
				var oTable = document.getElementsByTagName('table')[0];
				for (i=0;i < oTable.rows.length; i++){										
					for (j=0;j < oTable.rows[0].cells.length; j++){
						oTable.rows[i].cells[j].style.color = "#d2d2d3";
						
						oTable.rows[i].cells[5].style.color = "#FFA500"; 
						oTable.rows[i].cells[6].style.color = "#FFA500";
						oTable.rows[i].cells[7].style.color = "#FFA500";						
					}
					oTable.rows[i].cells[iCol].style.color = "#FF6633";					
				}

				otagsA = oTable.getElementsByTagName("a");  //a变色
				for (i = 0; i < otagsA.length; i++) {
					otagsA[i].style.color = "#d2d2d3";
					if(otagsA[i].id >= 6){
						otagsA[i].style.color = "#FFA500";								
					}
					if(otagsA[i].id == iCol + 1){
						otagsA[i].style.color = "#FF6633";
					}
				}								
			}
			
		</script>
	</head>
	<body>
		<div id="towrite" class="fixedBox"></div>
	</body>
</html>